package com.yanqu.road.server.manger.platform;

import com.yanqu.road.entity.platform.PlatformUserBehavior;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlatformUserBehaviorMgr extends TempMgr {

    static Map<Integer, Map<String, List<PlatformUserBehavior>>> behaviorMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        //int today = Integer.valueOf(DateHelper.getPyyyyMMddDateString(System.currentTimeMillis()));
        //behaviorMap = new PlatformUserBehaviorDaoImpl().getPlatformUserBehavior(today);
        behaviorMap = new ConcurrentHashMap<>();
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        if(behaviorMap != null){
            int today = Integer.valueOf(DateHelper.getPyyyyMMddDateString(System.currentTimeMillis() / 1000));
            List<Integer> removeList = new ArrayList<>();
            for (Map.Entry<Integer, Map<String, List<PlatformUserBehavior>>> entry : behaviorMap.entrySet()) {
                if(entry.getKey() < 1000){
                    //这类不移除
                    continue;
                }
                if(entry.getKey() != today){
                    removeList.add(entry.getKey());
                    continue;
                }
//                for (Map.Entry<String, List<PlatformUserBehavior>> listEntry : entry.getValue().entrySet()) {
//                    for (int i = 0; i < listEntry.getValue().size(); i++) {
//                        PlatformUserBehavior data = listEntry.getValue().get(i);
//                        if(data.isInsertOption()){
//                            new PlatformUserBehaviorDaoImpl().add(data);
//                        }
//                    }
//                }
            }
            for (Integer key : removeList) {
                behaviorMap.remove(key);
            }
        }
        return true;
    }

    public static boolean addUserBehavior(String uid, int type, int today) {
        Map<String, List<PlatformUserBehavior>> todayMap = behaviorMap.get(today);
        if(todayMap == null){
            synchronized (behaviorMap){
                todayMap = behaviorMap.get(today);
                if(todayMap == null){
                    behaviorMap.put(today, new ConcurrentHashMap<>());
                    todayMap = behaviorMap.get(today);
                }
            }
        }
        List<PlatformUserBehavior> list = todayMap.get(uid);
        if(list == null){
            synchronized (todayMap){
                list = todayMap.get(uid);
                if(list == null){
                    todayMap.put(uid, new ArrayList<>());
                    list = todayMap.get(uid);
                }
            }
        }
        synchronized (list){
            boolean exist = false;
            for (PlatformUserBehavior behavior : list) {
                if(behavior.getbDay() == today && behavior.getbType() == type){
                    exist = true;
                    break;
                }
            }
            if(!exist) {
                PlatformUserBehavior behavior = new PlatformUserBehavior();
                behavior.setInsertOption();
                behavior.setbDay(today);
                behavior.setbType(type);
                behavior.setPlatformUid(uid);
                list.add(behavior);
                return true;
            }
        }

        return false;
    }

    public static List<PlatformUserBehavior> getBehavior(String uid, int today){
        List<PlatformUserBehavior> result = new ArrayList<>();
        if(!behaviorMap.containsKey(today)){
            return result;
        }
        Map<String, List<PlatformUserBehavior>> todayMap = behaviorMap.get(today);
        if(!todayMap.containsKey(uid)){
            return result;
        }
        List<PlatformUserBehavior> list = todayMap.get(uid);
        if(list == null){
            return result;
        }
        for (PlatformUserBehavior behavior : list) {
            if(behavior.getbDay() == today){
                result.add(behavior);
            }
        }
        return result;
    }
}
